package system

import (
	"context"

	"portal/internal/svc"
	"portal/internal/tool/auth"
	"portal/internal/types"
	"portal/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListPostLogic struct {
	logx.Logger
	ctx       context.Context
	svcCtx    *svc.ServiceContext
	menuModel model.SysMenuModel
	postModel model.SysPostModel
}

func NewListPostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListPostLogic {
	return &ListPostLogic{
		Logger:    logx.WithContext(ctx),
		ctx:       ctx,
		svcCtx:    svcCtx,
		menuModel: model.NewSysMenuModel(svcCtx.Orm),
		postModel: model.NewSysPostModel(svcCtx.Orm),
	}
}

func (l *ListPostLogic) ListPost(req *types.ListPostRequest) (resp *types.CommonResponse, err error) {
	// todo: add your logic here and delete this line
	err = auth.HasPermission(l.menuModel, l.ctx, "system:post:query")
	if err != nil {
		return nil, err
	}
	postdtos, err := l.postModel.GetListByCondition(l.ctx, nil, req)
	if err != nil {
		return nil, err
	}
	postvos := make([]*types.Post, 0)
	for _, val := range postdtos {
		postvos = append(postvos, &types.Post{
			PostId:     val.PostId,
			PostCode:   val.PostCode,
			PostName:   val.PostName,
			PostSort:   val.PostSort,
			Status:     val.Status,
			CreateBy:   val.CreateBy,
			CreateTime: val.CreateTime,
			UpdateTime: val.UpdateTime,
			UpdateBy:   val.UpdateBy,
		})
	}
	total, err := l.postModel.CountCondition(l.ctx, nil, req)
	if err != nil {
		return nil, err
	}
	page := &types.PageParamResponse{
		Total: total,
		Size:  req.PageSize,
	}
	return &types.CommonResponse{
		Status: 200,
		Msg:    "查询成功",
		Data: &types.ListPostResponse{
			Posts:             postvos,
			PageParamResponse: *page,
		},
	}, nil
}
